
package com.wyzz.hr.bo.impl;

import com.wyzz.hr.action.ClientLimit;
import com.wyzz.hr.bo.IClientBO;
import com.wyzz.hr.dao.IClientDAO;
import com.wyzz.hr.pojo.Client;
import com.wyzz.hr.pojo.ContractType;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Ecptype;
import com.wyzz.hr.pojo.Emailtemplate;
import com.wyzz.hr.pojo.Empaddconf;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empsalarydatadef;
import com.wyzz.hr.pojo.Emptype;
import com.wyzz.hr.pojo.Infoclass;
import com.wyzz.hr.pojo.JobTitle;
import com.wyzz.hr.pojo.Jobgrade;
import com.wyzz.hr.pojo.Leavetype;
import com.wyzz.hr.pojo.Location;
import com.wyzz.hr.pojo.Overtimetype;
import com.wyzz.hr.pojo.Role;
import com.wyzz.hr.pojo.Sysconfig;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.DateUtil;
import com.wyzz.hr.util.MyTools;
import com.wyzz.hr.util.Pager;
import com.wyzz.hr.util.StringUtil;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.*;


public class ClientBOImpl implements IClientBO {

	public ClientBOImpl() {
	}

	public Client loadOneClient(String clientNo) {
		Object obj = clientDao.loadObject(com.wyzz.hr.pojo.Client.class,
				clientNo, null, new boolean[0]);
		if (obj != null)
			return (Client) obj;
		else
			return null;
	}

	public List loadClients() {
		return clientDao.getObjects(com.wyzz.hr.pojo.Client.class, null);
	}

	public void insertClient(Client newClient) {
		clientDao.saveObject(newClient);
	}

	public void deleteClient(String clientNo) {
		Client client = loadOneClient(clientNo);
		if (client != null)
			clientDao.deleteObject(client);
	}

	public void updateClient(Client client) {
		Client clientTmp = loadOneClient(client.getId());
		clientTmp.setClientName(client.getClientName());
		clientTmp.setClientShortName(client.getClientShortName());
		clientTmp.setClientAddress(client.getClientAddress());
		clientTmp.setClientZip(client.getClientZip());
		clientTmp.setClientPhone(client.getClientPhone());
		clientTmp.setClientFax(client.getClientFax());
		clientTmp.setClientEmail(client.getClientEmail());
		clientTmp.setClientContactName(client.getClientContactName());
		clientTmp.setClientRemarks(client.getClientRemarks());
		clientTmp.setClientStatus(client.getClientStatus());
		if (client.getClientLimit() != null)
			clientTmp.setClientLimit(client.getClientLimit());
		clientDao.updateObject(clientTmp);
	}

	public String checkEmpLimit(int addNum) {
		HttpSession session = ServletActionContext.getRequest().getSession();
		String limitStr = session.getAttribute("EMP").toString();
		int limit = Integer.parseInt(limitStr);
		if (addNum >= limit)
			return "overLimitE";
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Employee.class);
		detachedCriteria.add(Restrictions.eq("empStatus", Integer.valueOf(1)));
		int exitNumber = clientDao.findRowCountByCriteria(detachedCriteria)
				+ addNum;
		if (exitNumber > limit)
			return "overLimitE";
		else
			return "SUCC";
	}

	public String checkUserLimit(Integer roleNos[], int addNum) {
		String userType = getRoleAuths(roleNos);
		return checkUserLimit(userType, addNum);
	}

	public String checkUserLimit(String userType, int addNum) {
		HttpSession session = ServletActionContext.getRequest().getSession();
		int limit = Integer.parseInt(session.getAttribute("USER").toString());
		Integer users[] = getCurrentUsers(Integer.valueOf(1));
		if (users[3].intValue() + addNum > limit)
			return "overLimitU";
		limit = Integer.parseInt(session.getAttribute("USERADM").toString());
		getClass();
		if (userType.equals("USERADM")) {
			if (users[0].intValue() + addNum > limit)
				return "overLimitUADM";
		} else {
			getClass();
			if (userType.equals("USERMGR")) {
				Object usersObj = session.getAttribute("USERMGR");
				if (usersObj != null)
					limit = Integer.parseInt(usersObj.toString());
				if (users[1].intValue() + addNum > limit)
					return "overLimitUMGR";
			}
		}
		return "success";
	}

	public Integer[] getCurrentUsers(Integer uiStatus) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Userinfo.class);
		detachedCriteria.add(Restrictions.eq("uiStatus", uiStatus));
		List uiList = clientDao.findByCriteria(detachedCriteria);
		Integer users[] = { Integer.valueOf(0), Integer.valueOf(0),
				Integer.valueOf(0), Integer.valueOf(0) };
		users[3] = Integer.valueOf(uiList.size());
		for (Iterator i$ = uiList.iterator(); i$.hasNext();) {
			Userinfo ui = (Userinfo) i$.next();
			String authType = StringUtil
					.authStringToType(ui.getUiAuthDecrypt());
			if (authType == "USERADM") {
				Integer ainteger[] = users;
				Integer integer = ainteger[0];
				Integer integer3 = ainteger[0] = Integer.valueOf(ainteger[0]
						.intValue() + 1);
				Integer _tmp = integer;
			} else if (authType == "USERMGR") {
				Integer ainteger1[] = users;
				Integer integer1 = ainteger1[1];
				Integer integer4 = ainteger1[1] = Integer.valueOf(ainteger1[1]
						.intValue() + 1);
				Integer _tmp1 = integer1;
			} else {
				Integer ainteger2[] = users;
				Integer integer2 = ainteger2[2];
				Integer integer5 = ainteger2[2] = Integer.valueOf(ainteger2[2]
						.intValue() + 1);
				Integer _tmp2 = integer2;
			}
		}

		return users;
	}

	public String getRoleAuths(Integer roleNos[]) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Role.class);
		detachedCriteria.add(Restrictions.in("roleNo", roleNos));
		List roleList = clientDao.findByCriteria(detachedCriteria);
		if (roleList == null || roleList.size() == 0)
			return "USEREMP";
		Set authSet = new HashSet();
		Set authSetTemp;
		for (Iterator i$ = roleList.iterator(); i$.hasNext(); authSet
				.addAll(authSetTemp)) {
			Role role = (Role) i$.next();
			authSetTemp = StringUtil.authStringToSet(role.getRoleAuthority());
		}

		return StringUtil.authSetToType(authSet);
	}

	public IClientDAO getClientDao() {
		return clientDao;
	}

	public void setClientDao(IClientDAO clientDao) {
		this.clientDao = clientDao;
	}

	public List searchAllClient(Client client, Pager pager) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Client.class);
		if (client == null) {
			detachedCriteria.addOrder(Order.asc(Client.PROP_ID));
			splitPage(detachedCriteria, pager);
			return decryptLimit(clientDao.findByCriteria(detachedCriteria,
					pager.getPageSize(), pager.getCurrentPage()));
		}
		if (client.getId() != null && client.getId().length() > 0)
			detachedCriteria.add(Restrictions.like(Client.PROP_ID,
					(new StringBuilder()).append("%").append(client.getId())
							.append("%").toString()));
		if (client.getClientAddress() != null
				&& client.getClientAddress().length() > 0)
			detachedCriteria.add(Restrictions.like(Client.PROP_CLIENT_ADDRESS,
					(new StringBuilder()).append("%").append(
							client.getClientAddress()).append("%").toString()));
		if (client.getClientEmail() != null
				&& client.getClientEmail().length() > 0)
			detachedCriteria.add(Restrictions.like(Client.PROP_CLIENT_EMAIL,
					(new StringBuilder()).append("%").append(
							client.getClientEmail()).append("%").toString()));
		if (client.getClientFax() != null && client.getClientFax().length() > 0)
			detachedCriteria.add(Restrictions.like(Client.PROP_CLIENT_FAX,
					(new StringBuilder()).append("%").append(
							client.getClientFax()).append("%").toString()));
		if (client.getClientName() != null
				&& client.getClientName().length() > 0)
			detachedCriteria.add(Restrictions.like(Client.PROP_CLIENT_NAME,
					(new StringBuilder()).append("%").append(
							client.getClientName()).append("%").toString()));
		if (client.getClientPhone() != null
				&& client.getClientPhone().length() > 0)
			detachedCriteria.add(Restrictions.like(Client.PROP_CLIENT_PHONE,
					(new StringBuilder()).append("%").append(
							client.getClientPhone()).append("%").toString()));
		detachedCriteria.addOrder(Order.asc(Client.PROP_ID));
		splitPage(detachedCriteria, pager);
		return decryptLimit(clientDao.findByCriteria(detachedCriteria, pager
				.getPageSize(), pager.getCurrentPage()));
	}

	private List decryptLimit(List list) {
		List newList = new ArrayList();
		Client client;
		for (Iterator itor = list.iterator(); itor.hasNext(); newList
				.add(client)) {
			client = (Client) itor.next();
			String clientString = MyTools.vigenere(client.getClientLimit(),
					MyTools.getUpKey(client.getId(), MyTools.STRING),
					MyTools.DECRYPT_MODE);
			client.setClientLimit(clientString);
		}

		list = null;
		return newList;
	}

	public String updateLoginLimit(Client client) {
		Hashtable clientTable = ClientLimit.getClientLimit(client);
		String dateLimitString = (String) clientTable.get("dateLimit");
		String countLimitString = (String) clientTable.get("countLimit");
		String currentDate = MyTools.getcurrdate();
		int activateDateUtil = DateUtil.dateDiff(
				client.getClientActivateTime(), new Date(), 5);
		if (countLimitString.length() > 0
				&& Integer.valueOf(countLimitString).intValue() <= 0
				|| dateLimitString.length() > 0
				&& dateLimitString.compareTo(currentDate) < 0
				|| client.getClientStatus().intValue() == 1
				&& activateDateUtil > 30) {
			client.setClientStatus(Integer.valueOf(7));
			updateClient(client);
			return "overLimit";
		}
		if (countLimitString.length() > 0) {
			int currentCount = Integer.valueOf(countLimitString).intValue();
			currentCount--;
			String limit = (new StringBuilder()).append("EMP:").append(
					(String) clientTable.get("EMP")).append("#USER:").append(
					(String) clientTable.get("USER")).append("#AUTH:").append(
					(String) clientTable.get("authLimit")).append("#DATE:")
					.append((String) clientTable.get("dateLimit")).append(
							"#COUNT:").append(String.valueOf(currentCount))
					.toString();
			String limitEncrypt = MyTools
					.vigenere(limit, MyTools.getUpKey(client.getClientId(),
							MyTools.STRING), MyTools.ENCRYPT_MODE);
			client.setClientLimit(limitEncrypt);
			updateClient(client);
		}
		return "normalLimit";
	}

	private void splitPage(DetachedCriteria detachedCriteria, Pager page) {
		int size = clientDao.findRowCountByCriteria(detachedCriteria);
		int pageSize = Integer.valueOf(
				DatabaseSysConfigManager.getInstance().getProperty(
						"sys.split.pages")).intValue();
		detachedCriteria.setProjection(null);
		page.init(size, pageSize);
		if (page.getOperate() != null
				&& "previous".equalsIgnoreCase(page.getOperate()))
			page.previous();
		if (page.getOperate() != null
				&& "next".equalsIgnoreCase(page.getOperate()))
			page.next();
		if (page.getOperate() != null
				&& "first".equalsIgnoreCase(page.getOperate()))
			page.first();
		if (page.getOperate() != null
				&& "last".equalsIgnoreCase(page.getOperate()))
			page.last();
	}

	public void insertRegistClient(Client client, String auths) {
		String clientLimit = MyTools.vigenere(auths, MyTools.getUpKey(client
				.getClientId(), MyTools.STRING), MyTools.ENCRYPT_MODE);
		client.setClientLimit(clientLimit);
		client.setClientActivateTime(new Date());
		client.setClientServiceMonths(Integer.valueOf(3));
		client.setClientStatus(Integer.valueOf(1));
		clientDao.saveObject(client);
	}

	public void insertFirstUser(Employee employee, Client client,
			String password, String auth, String role) {
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.setAttribute("loginModel", "mutiple");
		session.setAttribute("clientNo", client.getId());
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.JobTitle.class);
		List objList = clientDao.findByCriteria(detachedCriteria);
		JobTitle jobTitle = null;
		if (objList != null && !objList.isEmpty()) {
			jobTitle = (JobTitle) objList.get(0);
		} else {
			jobTitle = new JobTitle();
			jobTitle
					.setJobtitleNameDesc("\u6CE8\u518C\u65F6\u81EA\u52A8\u6DFB\u52A0\u3002");
			jobTitle.setJobtitleSortId(Integer.valueOf(0));
			jobTitle.setClientNo(client.getId());
			jobTitle.setJobtitleNeedGmApprove(Integer.valueOf(0));
			clientDao.saveObject(jobTitle);
		}
		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Department.class);
		detachedCriteria.add(Restrictions.eq("departmentName", employee
				.getEmpDeptNo().getDepartmentName().trim()));
		objList = clientDao.findByCriteria(detachedCriteria);
		Department department = null;
		if (objList != null && !objList.isEmpty()) {
			department = (Department) objList.get(0);
		} else {
			department = new Department();
			department.setDepartmentName(employee.getEmpDeptNo()
					.getDepartmentName().trim());
			department
					.setDepartmentDesc("\u6CE8\u518C\u65F6\u81EA\u52A8\u6DFB\u52A0\u3002");
			department.setDepartmentSortId(Integer.valueOf(0));
			department.setClientNo(client.getId());
			clientDao.saveObject(department);
		}
		employee.setEmpDeptNo(department);
		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Emptype.class);
		detachedCriteria.add(Restrictions.eq("emptypeName", employee
				.getEmpType().getEmptypeName().trim()));
		objList = clientDao.findByCriteria(detachedCriteria);
		Emptype emptype = null;
		if (objList != null && !objList.isEmpty()) {
			emptype = (Emptype) objList.get(0);
		} else {
			emptype = new Emptype();
			emptype.setEmptypeName(employee.getEmpType().getEmptypeName()
					.trim());
			emptype
					.setEmptypeDesc("\u6CE8\u518C\u65F6\u81EA\u52A8\u6DFB\u52A0\u3002");
			emptype.setEmptypeSortId(Integer.valueOf(0));
			emptype.setClientNo(client.getId());
			clientDao.saveObject(emptype);
		}
		employee.setEmpType(emptype);
		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Location.class);
		detachedCriteria.add(Restrictions.eq("locationName", employee
				.getEmpLocationNo().getLocationName().trim()));
		objList = clientDao.findByCriteria(detachedCriteria);
		Location location = null;
		if (objList != null && !objList.isEmpty()) {
			location = (Location) objList.get(0);
		} else {
			location = new Location();
			location.setLocationName(employee.getEmpLocationNo()
					.getLocationName().trim());
			location.setLocationSortId(Integer.valueOf(0));
			location.setClientNo(client.getId());
			location
					.setLocationDesc("\u6CE8\u518C\u65F6\u81EA\u52A8\u6DFB\u52A0\u3002");
			clientDao.saveObject(location);
		}
		employee.setEmpLocationNo(location);
		employee.setId(UUID.randomUUID().toString());
		employee.setEmpStatus(Integer.valueOf(1));
		if (employee.getManager() == null)
			employee.setManager(employee.getId());
		employee.setEmpImportByInterface(Integer.valueOf(0));
		employee.setEmpCreateBy(employee);
		employee.setEmpCreateTime(Calendar.getInstance().getTime());
		employee.setEmpLastChangeTime(Calendar.getInstance().getTime());
		employee.setClientNo(client.getId());
		clientDao.saveObject(employee);
		Userinfo user = new Userinfo();
		user.setId(employee.getId());
		user.setUiUsername(employee.getEmpDistinctNo());
		user.setUiReferenceId(user.getUiUsername());
		user.setUiPasswordEncrypt(StringUtil.encodePassword(password));
		user.setUiAuthEncrypt(auth);
		user.setUiRoleEncrypt(role);
		user.setUiStatus(Integer.valueOf(1));
		user.setUiLevelRestrict(Integer.valueOf(0));
		user.setUiCreateBy(employee);
		user.setUiCreateTime(new Date());
		user.setUiLastChangeBy(employee);
		user.setUiLastChangeTime(new Date());
		user.setClientNo(client.getId());
		user.setEmployee(employee);
		clientDao.saveObject(user);
	}

	public String searchMaxClientId() {
		List oldClientList = clientDao.exeHqlList((new StringBuilder()).append(
				"select max(").append(Client.PROP_ID).append(") from Client")
				.toString());
		String oldClientId = oldClientList.get(0).toString();
		int maxClientId = Integer.parseInt(oldClientId) + 1;
		if (maxClientId < 101)
			maxClientId = 101;
		String clientId = String.valueOf(maxClientId);
		int length = oldClientId.length() - clientId.length();
		for (int idLength = 0; idLength < length; idLength++)
			clientId = (new StringBuilder()).append("0").append(clientId)
					.toString();

		return clientId;
	}

	public void insertDefaultTables(String clientModel, String clientNo) {
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.setAttribute("loginModel", "mutiple");
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Department.class);
		session.setAttribute("clientNo", clientModel);
		List objList = clientDao.findByCriteria(detachedCriteria);
		session.setAttribute("clientNo", clientNo);
		for (; !objList.isEmpty(); objList.remove(0)) {
			Department department = (Department) objList.get(0);
			Department newDepartment = new Department();
			newDepartment.setDepartmentDesc(department.getDepartmentDesc());
			newDepartment.setDepartmentName(department.getDepartmentName());
			newDepartment.setDepartmentSortId(department.getDepartmentSortId());
			clientDao.saveObject(newDepartment);
		}

		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Ecptype.class);
		session.setAttribute("clientNo", clientModel);
		objList = clientDao.findByCriteria(detachedCriteria);
		session.setAttribute("clientNo", clientNo);
		for (; !objList.isEmpty(); objList.remove(0)) {
			Ecptype ecptype = (Ecptype) objList.get(0);
			Ecptype newEcptype = new Ecptype();
			newEcptype.setEcptypeDesc(ecptype.getEcptypeDesc());
			newEcptype.setEcptypeName(ecptype.getEcptypeName());
			newEcptype.setEcptypeSortId(ecptype.getEcptypeSortId());
			clientDao.saveObject(newEcptype);
		}

		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.ContractType.class);
		session.setAttribute("clientNo", clientModel);
		objList = clientDao.findByCriteria(detachedCriteria);
		session.setAttribute("clientNo", clientNo);
		for (; !objList.isEmpty(); objList.remove(0)) {
			ContractType contracttype = (ContractType) objList.get(0);
			ContractType newContracttype = new ContractType();
			newContracttype.setEctDescription(contracttype.getEctDescription());
			newContracttype.setEctName(contracttype.getEctName());
			newContracttype.setEctSortId(contracttype.getEctSortId());
			clientDao.saveObject(newContracttype);
		}

		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Emailtemplate.class);
		session.setAttribute("clientNo", clientModel);
		objList = clientDao.findByCriteria(detachedCriteria);
		session.setAttribute("clientNo", clientNo);
		for (; !objList.isEmpty(); objList.remove(0)) {
			Emailtemplate emailtemplate = (Emailtemplate) objList.get(0);
			Emailtemplate newEmailtemplate = new Emailtemplate();
			newEmailtemplate.setEtContent(emailtemplate.getEtContent());
			newEmailtemplate.setEtNotes(emailtemplate.getEtNotes());
			newEmailtemplate.setEtStatus(emailtemplate.getEtStatus());
			newEmailtemplate.setEtTitle(emailtemplate.getEtTitle());
			newEmailtemplate.setEtTitleNo(emailtemplate.getEtTitleNo());
			clientDao.saveObject(newEmailtemplate);
		}

		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empaddconf.class);
		session.setAttribute("clientNo", clientModel);
		objList = clientDao.findByCriteria(detachedCriteria);
		session.setAttribute("clientNo", clientNo);
		for (; !objList.isEmpty(); objList.remove(0)) {
			Empaddconf empaddconf = (Empaddconf) objList.get(0);
			Empaddconf newEmpaddconf = new Empaddconf();
			newEmpaddconf.setEadcComments(empaddconf.getEadcComments());
			newEmpaddconf.setEadcFieldName(empaddconf.getEadcFieldName());
			newEmpaddconf.setEadcFieldType(empaddconf.getEadcFieldType());
			newEmpaddconf.setEadcFieldValue(empaddconf.getEadcFieldValue());
			newEmpaddconf.setEadcSeq(empaddconf.getEadcSeq());
			newEmpaddconf.setEadcTableName(empaddconf.getEadcTableName());
			clientDao.saveObject(newEmpaddconf);
		}

		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Emptype.class);
		session.setAttribute("clientNo", clientModel);
		objList = clientDao.findByCriteria(detachedCriteria);
		session.setAttribute("clientNo", clientNo);
		for (; !objList.isEmpty(); objList.remove(0)) {
			Emptype emptype = (Emptype) objList.get(0);
			Emptype newEmptype = new Emptype();
			newEmptype.setEmptypeDesc(emptype.getEmptypeDesc());
			newEmptype.setEmptypeName(emptype.getEmptypeName());
			newEmptype.setEmptypeSortId(emptype.getEmptypeSortId());
			clientDao.saveObject(newEmptype);
		}

		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Infoclass.class);
		session.setAttribute("clientNo", clientModel);
		objList = clientDao.findByCriteria(detachedCriteria);
		session.setAttribute("clientNo", clientNo);
		for (; !objList.isEmpty(); objList.remove(0)) {
			Infoclass infoclass = (Infoclass) objList.get(0);
			Infoclass newInfoclass = new Infoclass();
			newInfoclass.setInfoclassName(infoclass.getInfoclassName());
			newInfoclass.setInfoclassSortId(infoclass.getInfoclassSortId());
			newInfoclass.setInfoclassStatus(infoclass.getInfoclassStatus());
			clientDao.saveObject(newInfoclass);
		}

		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Jobgrade.class);
		session.setAttribute("clientNo", clientModel);
		objList = clientDao.findByCriteria(detachedCriteria);
		session.setAttribute("clientNo", clientNo);
		for (; !objList.isEmpty(); objList.remove(0)) {
			Jobgrade jobgrade = (Jobgrade) objList.get(0);
			Jobgrade newJobgrade = new Jobgrade(UUID.randomUUID().toString(),
					jobgrade.getJobGradeNo(), jobgrade.getJobGradeLevel(),
					jobgrade.getJobGradeMrp(), jobgrade.getJobGradeName());
			newJobgrade.setJobGradeSortId(Integer.valueOf(0));
			clientDao.saveObject(newJobgrade);
		}

		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.JobTitle.class);
		session.setAttribute("clientNo", clientModel);
		objList = clientDao.findByCriteria(detachedCriteria);
		session.setAttribute("clientNo", clientNo);
		for (; !objList.isEmpty(); objList.remove(0)) {
			JobTitle jobtitle = (JobTitle) objList.get(0);
			JobTitle newJobTitle = new JobTitle();
			newJobTitle.setJobtitleNo(jobtitle.getJobtitleNo());
			newJobTitle.setJobtitleName(jobtitle.getJobtitleName());
			newJobTitle.setJobtitleNameDesc(jobtitle.getJobtitleNameDesc());
			newJobTitle.setJobtitleNeedGmApprove(jobtitle
					.getJobtitleNeedGmApprove());
			newJobTitle.setJobtitleSortId(jobtitle.getJobtitleSortId());
			clientDao.saveObject(newJobTitle);
		}

		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Leavetype.class);
		session.setAttribute("clientNo", clientModel);
		objList = clientDao.findByCriteria(detachedCriteria);
		session.setAttribute("clientNo", clientNo);
		for (; !objList.isEmpty(); objList.remove(0)) {
			Leavetype leavetype = (Leavetype) objList.get(0);
			Leavetype newLeavetype = new Leavetype();
			newLeavetype.setLtSpecialCat(leavetype.getLtSpecialCat());
			newLeavetype.setLtDesc(leavetype.getLtDesc());
			newLeavetype.setLtInclHoliday(leavetype.getLtInclHoliday());
			newLeavetype.setLtMaxApplyDays(leavetype.getLtMaxApplyDays());
			newLeavetype.setLtName(leavetype.getLtName());
			newLeavetype.setLtSortId(leavetype.getLtSortId());
			clientDao.saveObject(newLeavetype);
		}

		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Location.class);
		session.setAttribute("clientNo", clientModel);
		objList = clientDao.findByCriteria(detachedCriteria);
		session.setAttribute("clientNo", clientNo);
		for (; !objList.isEmpty(); objList.remove(0)) {
			Location location = (Location) objList.get(0);
			Location newLocation = new Location();
			newLocation.setLocationDesc(location.getLocationDesc());
			newLocation.setLocationName(location.getLocationName());
			newLocation.setLocationSortId(location.getLocationSortId());
			clientDao.saveObject(newLocation);
		}

		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Overtimetype.class);
		session.setAttribute("clientNo", clientModel);
		objList = clientDao.findByCriteria(detachedCriteria);
		session.setAttribute("clientNo", clientNo);
		for (; !objList.isEmpty(); objList.remove(0)) {
			Overtimetype overtimetype = (Overtimetype) objList.get(0);
			Overtimetype newOvertimetype = new Overtimetype();
			newOvertimetype.setOtDesc(overtimetype.getOtDesc());
			newOvertimetype.setOtHourRate(overtimetype.getOtHourRate());
			newOvertimetype.setOtName(overtimetype.getOtName());
			newOvertimetype.setOtOverLimit(overtimetype.getOtOverLimit());
			newOvertimetype.setOtSortId(overtimetype.getOtSortId());
			clientDao.saveObject(newOvertimetype);
		}

		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Role.class);
		session.setAttribute("clientNo", clientModel);
		objList = clientDao.findByCriteria(detachedCriteria);
		session.setAttribute("clientNo", clientNo);
		for (; !objList.isEmpty(); objList.remove(0)) {
			Role role = (Role) objList.get(0);
			Role newRole = new Role();
			newRole.setRoleAuthority(role.getRoleAuthority());
			newRole.setRoleDesc(role.getRoleDesc());
			newRole.setRoleNo(role.getRoleNo());
			newRole.setRoleName(role.getRoleName());
			newRole.setRoleSortId(role.getRoleSortId());
			clientDao.saveObject(newRole);
		}

		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empsalarydatadef.class);
		session.setAttribute("clientNo", clientModel);
		objList = clientDao.findByCriteria(detachedCriteria);
		session.setAttribute("clientNo", clientNo);
		for (; !objList.isEmpty(); objList.remove(0)) {
			Empsalarydatadef empsalarydatadef = (Empsalarydatadef) objList
					.get(0);
			Empsalarydatadef newEmpsalarydatadef = new Empsalarydatadef();
			newEmpsalarydatadef.setEsddName(empsalarydatadef.getEsddName());
			newEmpsalarydatadef.setEsddDesc(empsalarydatadef.getEsddDesc());
			newEmpsalarydatadef.setEsddDataType(empsalarydatadef
					.getEsddDataType());
			newEmpsalarydatadef.setEsddDataIsCalc(empsalarydatadef
					.getEsddDataIsCalc());
			newEmpsalarydatadef.setEsddDataRounding(empsalarydatadef
					.getEsddDataRounding());
			newEmpsalarydatadef.setEsddDataLength(empsalarydatadef
					.getEsddDataLength());
			newEmpsalarydatadef.setEsddSortId(empsalarydatadef.getEsddSortId());
			newEmpsalarydatadef.setEsddDataOutput(empsalarydatadef
					.getEsddDataOutput());
			clientDao.saveObject(newEmpsalarydatadef);
		}

		detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Sysconfig.class);
		session.setAttribute("clientNo", clientModel);
		objList = clientDao.findByCriteria(detachedCriteria);
		session.setAttribute("clientNo", clientNo);
		for (; !objList.isEmpty(); objList.remove(0)) {
			Sysconfig sysconfig = (Sysconfig) objList.get(0);
			Sysconfig newSysconfig = new Sysconfig();
			newSysconfig.setSysconfigId(UUID.randomUUID().toString());
			newSysconfig.setSysconfigKey(sysconfig.getSysconfigKey());
			newSysconfig.setSysconfigValue(sysconfig.getSysconfigValue());
			clientDao.saveObject(newSysconfig);
		}

		Sysconfig newSysconfig = new Sysconfig();
		newSysconfig.setSysconfigId(UUID.randomUUID().toString());
		newSysconfig.setSysconfigKey("sys.file.path");
		newSysconfig.setSysconfigValue((new StringBuilder()).append(clientNo)
				.append("/file").toString());
		clientDao.saveObject(newSysconfig);
		session.setAttribute("clientNo", null);
	}

	public Client loadOneClientByClientId(String clientId) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Client.class);
		detachedCriteria.add(Restrictions.eq(Client.PROP_CLIENT_ID, clientId));
		List result = clientDao.findByCriteria(detachedCriteria);
		if (result == null || result.isEmpty())
			return null;
		else
			return (Client) result.get(0);
	}

	private IClientDAO clientDao;
	public final String USERADM = "USERADM";
	public final String USERMGR = "USERMGR";
	public final String USEREMP = "USEREMP";
	public final String USER = "USER";
	public final String EMP = "EMP";
}
